package com.yunquan.ohana.ui.photos.preview.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.github.chrisbanes.photoview.PhotoView
import com.google.gson.Gson
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder
import com.yunquan.ohana.R
import com.yunquan.ohana.entity.PhotosEntity
import com.yunquan.ohana.ui.photos.preview.PreviewActivity
import com.yunquan.ohana.utils.L
import com.yunquan.ohana.view.SampleCoverVideo
import java.io.File

class PreviewAdapter(
    private val activity: PreviewActivity,
    private val dataList: List<PhotosEntity>
) : RecyclerView.Adapter<PreviewAdapter.ViewHolder>() {


    override fun getItemCount(): Int {
        return if (dataList.isEmpty()) 0 else Int.MAX_VALUE
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_preview, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val index = position % dataList.size
        val item = dataList[index]
        if (item.type!!.contains("image")) {
            holder.showImage(item)
        } else {
            holder.playVideo(item)
        }
    }

    inner class ViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
        private val ivPic: PhotoView = view.findViewById(R.id.iv_content)
        val videoView: SampleCoverVideo = view.findViewById(R.id.video_content)

        fun showImage(item: PhotosEntity) {
            ivPic.visibility = View.VISIBLE
            Glide.with(view.context).load(item.path).into(ivPic)
        }

        fun playVideo(item: PhotosEntity) {
            videoView.visibility = View.VISIBLE
            // 背景图
            val thumbImageView = ImageView(view.context)
            if (item.thumbnail != null) {
                val file = File(item.thumbnail)
                Glide.with(view.context).load(file).into(thumbImageView)
            }
            videoView.thumbImageView = thumbImageView
            // 播放视频
            val builder = GSYVideoOptionBuilder()
            builder.setUrl(item.path)
                .setCacheWithPlay(false)
                .setLooping(false)
                .setAutoFullWithSize(false)
                .setShowFullAnimation(true)
                .setReleaseWhenLossAudio(false)
                .setThumbPlay(true)
                .setNeedLockFull(true)
                .setIsTouchWigetFull(true)
                .setIsTouchWiget(true)
                .setHideKey(false)
                .build(videoView)
            videoView.titleTextView.visibility = View.GONE
            videoView.backButton.visibility = View.GONE
            videoView.fullscreenButton.setOnClickListener {
                videoView.startWindowFullscreen(view.context, false, false)
            }
        }
    }
}